package gui.batches;

import core.Container;
import core.Item;
import core.ItemFacade;
import core.ProductFacade;
import core.exception.ModelException;

public class TransferItemCommand implements Command {
	private Item _item;
	private Container _oldContainer;
	private Container _newContainer;
	private boolean _addedProduct;
	/**
	 * Construct a transfer item command
	 * @param i The item to be transferred
	 * @param fromContainer The container the item is currently in.
	 * @param toContainer The container the item is going to.
	 */
	public TransferItemCommand(Item i, Container fromContainer, Container toContainer){
		assert(i != null);
		_item = i;
		_oldContainer = fromContainer;
		_newContainer = toContainer;
		_addedProduct = false;
	}
	
	@Override
	public void execute() throws ModelException {
		if (_newContainer.getStorageUnit().getContainerForProduct(_item.getProduct()) == null)
			_addedProduct = true;	
		ItemFacade.instance().transferItem(_item, _newContainer);

	}

	@Override
	public void undo() throws ModelException{
		ItemFacade.instance().transferItem(_item, _oldContainer);
		if (_addedProduct)
			ProductFacade.instance().removeProduct(_item.getProduct(), _newContainer);

	}

}
